package com.example.gobang.game;

import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class RoomManger {
    private ConcurrentHashMap<String,Room> rooms = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer,String> userIdRooms = new ConcurrentHashMap<>();
    public void add(Room room,int userId1,int userId2){
        rooms.put(room.getRoomId(),room);
        userIdRooms.put(userId1,room.getRoomId());
        userIdRooms.put(userId2,room.getRoomId());
    }
    public void remove(String roomId,int userId1,int userId2){
        rooms.remove(roomId);
        userIdRooms.remove(userId1);
        userIdRooms.remove(userId2);
    }
    public Room getRoom(String roomId){
        return rooms.get(roomId);
    }
    public   Room getRoomBYUserId(int userId){
        String roomId = userIdRooms.get(userId);
        if(roomId==null){
            return null;
        }
        return  rooms.get(roomId);
    }
}
